//
//  ChooseAreaView.m
//  ZhuanZuBao
//
//  Created by bchy on 2020/9/14.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "ChooseAreaView.h"

@interface ChooseAreaView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation ChooseAreaView



-(instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.4];
    
    _tableView = [UITableView new];
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorColor = SEPARATECOLOR;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.height.mas_equalTo(DEVICE_SCREEN_HEIGHT-220-SafeAreaTopHeight);
    }];
    
    UIButton *cancelBtn = [UIButton new];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.top.equalTo(self.tableView.mas_bottom);
    }];
}

-(void)setCity:(NSString *)city{
    //设置城市，加载区域
    [NetWork loadCitysWithKeyWord:city subdistrict:1 success:^(id responseBody) {
        NSLog(@"%@", responseBody);
        self.dataArr = [NSMutableArray array];
        [self.dataArr addObjectsFromArray:responseBody];
        //在第一个加上整个q市的选项
        NSDictionary *dict = @{
            @"name": [NSString stringWithFormat:@"全%@", city]
        };
        [self.dataArr insertObject:dict atIndex:0];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    if (_selectedIndex == indexPath.row) {
        cell.textLabel.textColor = THEMECOLOR;
    }else{
        cell.textLabel.textColor = BLACKTEXTCOLOR;
    }
    NSDictionary *dict = _dataArr[indexPath.row];
    cell.textLabel.text = dict[@"name"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selectedIndex = indexPath.row;
    [self.tableView reloadData];
    if (indexPath.row == 0) {
        if (self.selectAreaBlock) {
            self.selectAreaBlock(@"");
        }
    }else{
        NSDictionary *dict = _dataArr[indexPath.row];
        NSString *district = dict[@"name"];
        if (self.selectAreaBlock) {
            self.selectAreaBlock(district);
        }
    }
    
    self.hidden = YES;
}

-(void)cancel{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    self.hidden = YES;
}

@end
